package com.lsy.weizhi2.activity;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.Classify;
import com.lsy.weizhi2.JsonObject.SelectLevel;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.CustomPage_GV_Adapter;
import com.lsy.weizhi2.adapter.CustomPage_LV_Adapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.CostomFlag;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.customview.AutoGridView;
import com.lsy.weizhi2.customview.AutoListView;
import com.lsy.weizhi2.fragment.FuJingFragment;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.mysql.MySQLOperate;
import com.lsy.weizhi2.myutils.CustomLVAdapterCall;
import com.lsy.weizhi2.myutils.TitleViewCall;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class CustomPage extends MyBaseActivity {
    private Context context;
    private int height;//title控件的高度，便于使用沉浸式效果
    private int xx;//辅助获取title高度
    private MyTitleView custom_page;
    private AutoGridView custom_already_agv;
    private CustomPage_GV_Adapter customPage_gv_adapter;
    private CustomPage_LV_Adapter customPage_lv_adapter;
    private AutoListView custom_wait_alv;
    public static int Tag;
    private Gson gson;
    private List<Classify> list;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_page);
        context=CustomPage.this;
        Init();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        xx=0;
        ViewTreeObserver vto = custom_page.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(custom_page.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                                custom_page.setLayoutParams(params);
                            }
                        }
                    });
                }
            }
        });
        MyOkHttpUtils.getInstance(context).doGet(Constants.GETORDERLIST,null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if(message!=null){

                            JSONArray jsonArray= null;
                            try {
                                jsonArray = new JSONArray(message);
                            } catch (JSONException e) {
                                Log.i("CustomPage","message=错误=1="+message);
                            }
                            list=new ArrayList<Classify>();
                            if(jsonArray!=null&&jsonArray.length()!=0){
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    Classify classify= null;
                                    try {
                                        classify = gson.fromJson(jsonArray.getJSONObject(i).toString(),Classify.class);
                                    } catch (JSONException e) {
                                        Log.i("CustomPage","message=错误=2="+message);
                                    }
                                    list.add(classify);
                                }
                                Log.i("CustomPage","list=="+list.size());
                                customPage_lv_adapter.UpData(list);
                            }
                        }
                    }
                });
    }
    @Override
    public void onPause() {
        super.onPause();
        xx=0;
    }
    private void Init() {
        gson=new Gson();
        custom_page= (MyTitleView) findViewById(R.id.custom_page);
        if(Tag==0){
            custom_page.setOk("编辑");
        }else{
            custom_page.setOk("确定");
        }
        custom_already_agv= (AutoGridView) findViewById(R.id.custom_already_agv);
        custom_wait_alv= (AutoListView) findViewById(R.id.custom_wait_alv);
        custom_page.setTitleViewCall(new TitleViewCall() {
            @Override
            public void Click(View view, int flag) {
                if(flag==TitleViewCall.OK){
                    if(custom_page.getOk().equals("编辑")&&Tag==0){
                        custom_page.setOk("确定");
                        custom_already_agv.setVisibility(View.VISIBLE);
                    }else if(custom_page.getOk().equals("确定")&&Tag==0){
                        custom_page.setOk("编辑");
                        custom_already_agv.setVisibility(View.GONE);
                        //点击确定写入数据库
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                int size=CostomFlag.list_select.size();
                                for (int i = 0; i < 9; i++) {
                                    if(i<size){
                                        ContentValues values=new ContentValues();
                                        values.put("first",CostomFlag.list_select.get(i).getFid());
                                        values.put("second",CostomFlag.list_select.get(i).getId());
                                        values.put("name",CostomFlag.list_select.get(i).getName());
                                        values.put("place",CostomFlag.list_select.get(i).getImage());
                                        values.put("id",i+1);
                                        MySQLOperate.getInstance(context).Insert("homegrid",values,i+1);
                                    }else{
                                        MySQLOperate.getInstance(context).Delete("homegrid",i+1);
                                    }
                                }
                            }
                        }).start();
                    }else if(Tag==1){
                        MyMethod.toActivity(context,SendNote.class);
                        finish();
                    }
                }
            }
        });
        customPage_gv_adapter=new CustomPage_GV_Adapter(context,null);
        custom_already_agv.setAdapter(customPage_gv_adapter);
        customPage_lv_adapter=new CustomPage_LV_Adapter(context,null);
        custom_wait_alv.setAdapter(customPage_lv_adapter);
        customPage_lv_adapter.setCustomLVAdapterCall(new CustomLVAdapterCall() {
            @Override
            public void onItem(View view, int position, int parent,String title) {
               // int x=customPage_gv_adapter.getX();
                int x=CostomFlag.list_select.size()+1;
                if(Tag==2){
                    MyMethod.toActivity(context,ClassifyActivity.class,new String[]{"first","second","title"},new String[]{parent+"",position+"",title});
                    finish();
                }else if(Tag==3){
                    FuJingFragment.first=parent;
                    FuJingFragment.second=position;
                    FuJingFragment.name=title;
                    MyMethod.toActivity(context,HomePage.class);
                    finish();
                }else{
                    if(x<10&&custom_already_agv.isShown()&&Tag==0){
                        int id=position;
                        String name=title;
                        int fid=parent;
                        for (int i = 0; i < CostomFlag.list_select.size(); i++) {
                           if(CostomFlag.list_select.get(i).getName().equals(name)){
                               MyMethod.showToast(context,"请勿重复添加");
                               return;
                           }
                        }
                        SelectLevel level=new SelectLevel();
                        level.setId(id);
                        level.setName(name);
                        level.setImage(Constants.All_images[id]);
                        level.setFid(fid);
                        CostomFlag.list_select.add(level);
                        customPage_gv_adapter.UpData();
                        Log.i("CustomPage","parent=="+parent+"\nposition=="+position+"\nid=="+id+"\nname=="+name);

                    }else if(Tag==1){
                        //MyMethod.showToast(context,"点击了parent=="+parent+"子==="+position);
                        SendNote.name=title;
                        SendNote.first_c=parent;
                        SendNote.second_c=position;
                        finish();
                    }
                }
            }
        });
    }
}
